<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<input type="hidden" name="aType" id="aType" value="${stepBrief.actionType}" />
<input type="hidden" name="sType" id="sType" value="${stepBrief.specialType}"  />
<input type="hidden" name="chooseId" id="chooseId" />
<div class="panel">

  <div class="panel-heading header">
    <h3 class="panel-title">基本信息</h3>
  </div>

      <form id="form_stepBrief_input" class="form-horizontal">
        <input type="hidden" name="parentId" id="parentId" value="${stepBrief.processBrief.id}" />
        <input type="hidden" name="keyId" id="keyId" value="${stepBrief.id}" />
        <input type="hidden" name="commonConfigId" id="commonConfigId" value="${stepBrief.commonConfig.id}" />

        <div class="form-group">
          <label class="col-md-2 control-label">步骤编号<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
            <input class="form-control" type="text"  name="stepNo" id="stepNo"  placeholder="请输入步骤编号"
                   value="<c:if test="${stepBrief.stepNo!=0}">${stepBrief.stepNo}</c:if>" />
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-2 control-label">步骤名称<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
              <input class="form-control" type="text"  name="stepName" id="stepName"  placeholder="请输入步骤名称"
                     value="${stepBrief.name}" />
          </div>
        </div>

        <div class="form-group">
          <label class="col-md-2 control-label">参与者权限</label>
          <div class="col-md-6">
            <div class="checkbox">
              <input type="checkbox"  id="back" name="actionType" value="1" class="magic-checkbox">
              <label for="back">允许退回</label>
              <input type="checkbox" id="deny" name="actionType" value="2"  class="magic-checkbox">
              <label for="deny">允许否决</label>
            </div>
          </div>
        </div>
        <div class="form-group" style="display:none" id="showBackStepNo">
          <label class="col-md-2 control-label">退回步骤编号<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
              <input class="form-control" type="text"  name="backStepNo" id="backStepNo"  placeholder="请输入退回步骤编号"
                     value="<c:if test="${stepBrief.backStepNo!=0}">${stepBrief.backStepNo}</c:if>" />
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-2 control-label">是否会签<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
            <div class="radio">
                <input type="radio" name="jointlySing" id="jointlySingYes" value="1"  class="magic-radio" <c:if test="${stepBrief.jointlySing==true}">checked</c:if> >
                <label for="jointlySingYes">是</label>
                <input type="radio" name="jointlySing" id="jointlySingNo" value="0" class="magic-radio" <c:if test="${stepBrief.jointlySing==false}">checked</c:if> >
                <label for="jointlySingNo">否</label>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label class="col-md-2 control-label">路由类型<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
              <div class="radio">
                <input type="radio" name="routeType" id="routeTypeCom" value="1"  class="magic-radio" <c:if test="${stepBrief.routeType != 2}">checked</c:if> />
                <label for="routeTypeCom">一般路由</label>
                <input type="radio" name="routeType" id="routeTypeCon" value="2" class="magic-radio" <c:if test="${stepBrief.routeType == 2}">checked</c:if> />
                <label for="routeTypeCon">条件路由</label>
              </div>
          </div>
        </div>

        <div class="form-group" id="div_nextStepNo" <c:if test="${stepBrief.routeType == 2}">style="display:none"</c:if>>
          <label class="col-md-2 control-label">下一步步骤编号(0代表归档)<i class="fa fa-asterisk txt-color-red"></i></label>
          <div class="col-md-6">
              <input  class="form-control" type="text" name="nextStepNo" id="nextStepNo"  placeholder="请输入下一步步骤编号"
                      value="${stepBrief.nextStepNo}">
          </div>
        </div>
        <div class="form-group" id="ajax_stepCon_list_row" <c:if test="${stepBrief.routeType != 2}">style="display:none"</c:if>>
          <label class="col-md-2 control-label">
            <a id="ajax_stepCon_btn_add"  class="btn btn-primary btn-sm" ><i class="ti-plus"> </i> 新建条件路由</a>
          </label>
          <div class="col-md-6">
            <table id="ajax_stepCon_table" class="table table-striped table-bordered table-hover">
            </table>
            <div id="ajax_stepCon_list_page">
            </div>
          </div>
        </div>

        <div class="panel-heading header">
          <h3 class="panel-title" style="display: inline-block">参与者信息</h3>
        </div>
        <div class="form-group">
          <label class="col-md-2 control-label">所有人： </label>
          <div class="col-md-6">
            <div class="checkbox">
                <input type="checkbox" value="1" name="allmans" id="allmans" <c:if test='${stepBrief.commonConfig.allmans==true}'>checked</c:if> class="magic-checkbox">
                <label for="allmans"></label>
            </div>
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-depart" style="color:#4FA5D0"> 部门： </a>
          </label>
          <div class="col-md-6">
            <input  disabled type="text" id="departName" class="form-control"
                    value="<c:forEach items="${stepBrief.commonConfig.departmentSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="departId" name="departId"
                   value="<c:forEach items="${stepBrief.commonConfig.departmentSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-post" style="color:#4FA5D0"> 岗位：</a>
          </label>
          <div class="col-md-6">
            <input disabled  type="text" id="postName" class="form-control"
                   value="<c:forEach items="${stepBrief.commonConfig.postSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="postId" name="postId"
                   value="<c:forEach items="${stepBrief.commonConfig.postSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-power" style="color:#4FA5D0">职权： </a>
          </label>
          <div class="col-md-6">
              <input disabled  type="text" id="powerName"  class="form-control"
                     value="<c:forEach items="${stepBrief.commonConfig.powerSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.department.name}-${list.post.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>" />
              <input type="hidden"  id="powerId" name="powerId"
                     value="<c:forEach items="${stepBrief.commonConfig.powerSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-md-2 control-label">
            <a  href="javascript:void(0);" key="btn-choose-group" style="color:#4FA5D0">群组：</a>
          </label>
          <div class="col-md-6">
            <input disabled type="text" id="groupName"  class="form-control"
                   value="<c:forEach items="${stepBrief.commonConfig.groupSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.name}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
            <input type="hidden" id="groupId" name="groupId"
                   value="<c:forEach items="${stepBrief.commonConfig.groupSet}" varStatus="i" var="list" ><c:if test="${list.state.toString() == 'Enable'}">${list.id}<c:if test="${!i.last}">,</c:if></c:if></c:forEach>"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-2 control-label">域名：</label>
          <div class="col-md-6">
            <select class="form-control" name="variableName" id="variableName">
              <option value="">--请选择--</option>
              <c:forEach items="${fieldNames}" varStatus="i" var="list">
                <option  value="${list}"  <c:if test='${list==stepBrief.commonConfig.variable}'>selected</c:if>>${list}</option>
              </c:forEach>
            </select>
          </div>
        </div>
        <div class="panel-heading header">
          <h3 class="panel-title" style="display: inline-block">特殊设定</h3>
        </div>

        <div class="form-group ">
          <label class="col-md-2 control-label"></label>
          <div class="col-md-6">
            <div class="checkbox">
                <input type="checkbox" id="power" name="specialType" value="1"  class="magic-checkbox">
                <label for="power">上一步操作用户职权上级</label>
                <input type="checkbox" id="header" name="specialType" value="2" class="magic-checkbox">
                <label for="header">上一步操作用户所属部门负责人</label>
                <input type="checkbox" id="upheader" name="specialType" value="3" class="magic-checkbox">
                <label for="upheader">上一步操作用户所属部门的上级部门负责人</label>
            </div>
          </div>
        </div>
      </form>
      <div class="panel-footer">
        <a class="btn btn-default" id="apply-re-common2" href="javascript:void(0)">
          <i class="fa fa-lg fa-mail-reply"></i> 返回视图
        </a>
        <a class="btn btn-warning" id="btn-submit-common2" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交表单 </a>
      </div>

</div>





<script>

  $(function(){
      fn_stepBrief_input_validate();
      load_stepCon_jqGrid();

      var aType = $('#aType').val();
      var sType = $('#sType').val();
      $("input[name='actionType']").each(function(i,v){
          var optVal = $(v).val();
          if(aType != "" && aType.indexOf(optVal) != -1){
              $(v).prop("checked",true);
          }
      });
      $(':checked[name="actionType"]').each(function(){
          if($(this).val() == "1"){
              $('#showBackStepNo').show();
              return false;
          }
      });
      $("input[name='specialType']").each(function(i,v){
          var optVal = $(v).val();
          if(sType != "" && sType.indexOf(optVal) != -1){
              $(v).prop("checked",true);
          }
      });

      $(':radio[name="routeType"]').click(function(){
          if($(this).val()==2){
              $('#div_nextStepNo').hide();
              $('#ajax_stepCon_list_row').show();
          }else{
              $('#div_nextStepNo').show();
              $('#ajax_stepCon_list_row').hide();
          }
      });

      $(":input[name='actionType']").click(function(){
          if($(this).val() == "1"){
              if($(this).is(":checked")){
                  $('#showBackStepNo').show();
              }else{
                  $('#showBackStepNo').hide();
              }
          }
      })

  });


  function load_stepCon_jqGrid(){
      jQuery("#ajax_stepCon_table").jqGrid({
          url:'<%=path%>/main/stepCon/list?parentId='+$('#form_stepBrief_input #keyId').val(),
          datatype: "json",
          colNames:['条件名称',"条件描述","条件公式","路由步骤","操作","id"],
          colModel:[
              {name:'name',index:'name',sortable:false,search:false,width:120},
              {name:'content',index:'content',sortable:false,search:false,width:160},
              {name:'formula',index:'formula',sortable:false,search:false,width:200},
              {name:'stepNo',index:'stepNo',sortable:false,search:false,width:70},
              {name:'act',index:'act', width:150,sortable:false,search:false,fixed:true},
              {name:'id',index:'id',search:false,hidden:true,key:true}
          ],
          rowNum : 10,
          rowList:[10,20,30],
          pager : '#ajax_stepCon_list_page',
          sortname : '',
          sortorder : "",
          gridComplete:function(){
              var ids=$("#ajax_stepCon_table").jqGrid('getDataIDs');
              for(var i=0;i<ids.length;i++){
                  var cl=ids[i];
                  var rowData = $("#ajax_stepCon_table").jqGrid("getRowData",cl);
                  var de="<a class='btn btn-default' data-toggle='modal' flag='edit' id='e"+cl+"' key='"+cl+"'><i class='fa fa-eye'></i>编辑</a>"+" ";
                  var dt="<a class='btn btn-default' data-toggle='modal' flag='del' id='d"+cl+"' key='"+cl+"'><i class='fa fa-eye'></i>删除</a>"+" ";
                  jQuery("#ajax_stepCon_table").jqGrid('setRowData',ids[i],{act:de+dt});
                  //编辑
                  $('#e'+cl).click(function(){
                      var id = $(this).attr('key');
                      __open_dialog_form('条件路由', '<%=path%>/main/stepCon/input?keyId='+id, function (dialogRef) {
                          var callback = function (data) {
                              if (data) {
                                  dialogRef.close();

                                  // 刷新grid
                                  $('#ajax_stepCon_table').trigger('reloadGrid');
                              } else {
                                  dialogRef.enableButtons();
                              }
                          };
                          if (fn_stepCon_save(callback) === false) {
                              dialogRef.enableButtons();
                          }
                      });
                      return false;

                  });
                  //删除
                  $('#d'+cl).click(function(){
                      var id = $(this).attr('key');
                      __confirm_dialog(null, '确定要删除吗?',
                          function () {
                              var url = "<%=path%>/main/stepCon/delete/"+id;
                              __ajax_post(url, null, function(pdata) {
                                  __toastr(pdata);
                                  jQuery("#ajax_stepCon_table").jqGrid().setGridParam({datatype:'json'}).trigger("reloadGrid");
                              });
                          });
                      return false;
                  });
              }
              $(".ui-jqgrid-bdiv").css("overflow-x","hidden");
          },
          onSelectRow: function (rowId, status, e) {

              var rowId = $("#ajax_stepCon_table").jqGrid('getGridParam','selrow');
              var rowDatas = $("#ajax_stepCon_table").jqGrid('getRowData', rowId);
              //$('#chooseId').val(rowId);
          },
          jsonReader: {
              root: "dataRows",
              page: "page",
              total: "total",
              records: "records",
              repeatitems : false
          },
          caption : "<i class='fa fa-arrow-circle-right'></i> 步骤条件",
          // multiselect : true,
          rownumbers:true,
          gridview:true,
          shrinkToFit:true,
          viewrecords: true,
          autowidth: true,
          height:'auto',
          forceFit:true,
          loadComplete: function() {
          }
      });
      $(window).on('resize.jqGrid', function() {
          jQuery("#ajax_stepCon_table").jqGrid('setGridWidth', $("#ajax_stepCon_list_row").width());
      })
      jQuery("#ajax_stepCon_table").jqGrid('filterToolbar',{searchOperators:false,stringResult:true});

      jQuery("#ajax_stepCon_table").jqGrid('navGrid', "#ajax_stepCon_list_page", {
          edit : false,
          add : false,
          del : false,
          search:false
      });
  };

  // 表单数据校验
  function fn_stepBrief_input_validate() {
    __init_validate('form_stepBrief_input', {
        rules : {
            stepNo : {
                required : true,
                digits:true
            },
            stepName:{
                required : true
            },
            backStepNo:{
                required: function(){
                    if($('#showBackStepNo').is(":visible")){
                        return true;
                    }else{
                        return false;
                    }
                },
                digits:true
            },
            jointlySing:{
                required:true
            },
            routeType:{
                required:true
            },
            nextStepNo:{
                required: function(){
                    if($(':checked[name="routeType"]').val() =="1"){
                        return true;
                    }else{
                        return false;
                    }
                },
                digits:true
            }
        },
        messages : {
            stepNo : {
                required : "请输入步骤编号",
                digits: "步骤编号必须为数字"
            },
            stepName:{
                required :"请输入步骤名称"
            },

            backStepNo:{
                required : "请输入退回步骤编号",
                digits: "退回步骤编号必须为数字"
            },
            jointlySing:{
                required:"请选择是否会签"
            },
            routeType:{
                required:"请选择路由类型"
            },
            nextStepNo:{
                required :"请输入下一步步骤编号",
                digits:"下一步步骤编号必须为数字"
            }
        }
    });
  }


  //新建条件路由
  $('#ajax_stepCon_btn_add').click(function(){
      if($('#form_stepBrief_input #keyId').val()==""){
          alert("请先保存步骤简要表");
          return false;
      }
      __open_dialog_form('条件路由', '<%=path%>/main/stepCon/input?parentId='+$('#form_stepBrief_input #keyId').val(), function (dialogRef) {
          var callback = function (data) {
              if (data) {
                  dialogRef.close();

                  // 刷新grid
                  $('#ajax_stepCon_table').trigger('reloadGrid');
              } else {
                  dialogRef.enableButtons();
              }
          };
          if (fn_stepCon_save(callback) === false) {
              dialogRef.enableButtons();
          }
      });
      return false;

  });




  //返回视图
  $("#apply-re-common2").click(function(){
      loadURL("menu/stepBrief", $('div#s2'));
  });

  //提交
  $("#btn-submit-common2").click(
      function(e) {
          if (!$("#form_stepBrief_input").valid()) {
              return false;
          }
          __confirm_dialog("提示：","确定提交吗？",
              function () {
                  __form_save("form_stepBrief_input", "<%=path%>/main/stepBrief/save", function(){
                      $("#apply-re-common2").trigger("click");
                  });
              }
          )
      }
  );


  // 岗位选择框
  function fn_post_dialog_select(nameField,idField) {

      __open_dialog_select('选择岗位', '<%=path%>/main/post/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
          var callback = function (data) {
              if (data) {
                  dialogRef.close();
                  $('#'+idField).val(data.ids);
                  $('#'+nameField).val(data.names);
              } else {
                  dialogRef.enableButtons();
              }
          };
          fn_dialog_post_select(callback);
      });
  }
  // 部门选择框
  function fn_departments_dialog_select(isMulti,nameField,idField) {

      __open_dialog_select('选择部门', '<%=path%>/main/department/dialog?multi='+isMulti+'&deptIds='+ $('#'+idField).val(), function (dialogRef) {
          var callback = function (data) {
              if (data) {
                  dialogRef.close();
                  $('#'+idField).val(data.ids);
                  $('#'+nameField).val(data.names);
              } else {
                  dialogRef.enableButtons();
              }
          };
          fn_dialog_departments_select(callback);
      });
  }
  // 职权选择框
  function fn_power_dialog_select(nameField,idField) {

      __open_dialog_select('选择职权', '<%=path%>/main/power/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
          var callback = function (data) {
              if (data) {
                  dialogRef.close();

                  $('#'+idField).val(data.ids);
                  $('#'+nameField).val(data.names);
              } else {
                  dialogRef.enableButtons();
              }
          };
          fn_dialog_power_select(callback);
      });
  }

  // 群组选择框
  function fn_group_dialog_select(nameField,idField) {

      __open_dialog_select('选择群组', '<%=path%>/main/group/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
          var callback = function (data) {
              if (data) {
                  dialogRef.close();

                  $('#'+idField).val(data.ids);
                  $('#'+nameField).val(data.names);
              } else {
                  dialogRef.enableButtons();
              }
          };
          fn_dialog_group_select(callback);
      });
  }


  $("a[key='btn-choose-group']").unbind("click").bind("click", function(){fn_group_dialog_select("groupName","groupId")} );
  $("a[key='btn-choose-depart']").unbind("click").bind("click", function(){fn_departments_dialog_select("y","departName","departId")});
  $("a[key='btn-choose-post']").unbind("click").bind("click", function(){fn_post_dialog_select("postName","postId")});
  $("a[key='btn-choose-power']").unbind("click").bind("click",  function(){fn_power_dialog_select("powerName","powerId")});
</script>